package com.zxtyshangjia.zxtyjms.order.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxtyshangjia.zxtyjms.R;
import com.zxtyshangjia.zxtyjms.app.Myappalication;
import com.zxtyshangjia.zxtyjms.order.bean.OrderData;
import com.zxtyshangjia.zxtyjms.utils.Api;

import java.util.List;

/**
 * Created by 18222 on 2017/11/15.
 */

public class OrderListAdapter  extends BaseAdapter{

    private Context ctx;
    private List<OrderData.OrderList> list;
    private LayoutInflater inflater;


    public OrderListAdapter(Context ctx,List<OrderData.OrderList> list) {
        this.ctx = ctx;
        this.list = list;
        inflater = LayoutInflater.from(ctx);
    }


    @Override
    public int getCount() {
        return list!=null? list.size():0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if(view == null){
            view = inflater.inflate(R.layout.order_list_item,null);
            holder = new ViewHolder();
            holder.mImg = (ImageView) view.findViewById(R.id.sj_img_iv);
            holder.mName = (TextView) view.findViewById(R.id.sj_name_tv);
            holder.mCreatTime = (TextView) view.findViewById(R.id.order_time_tv);
            holder.mNum = (TextView) view.findViewById(R.id.order_num_tv);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        OrderData.OrderList item = list.get(i);

        if(item.head_pic.indexOf("https://") != -1 || item.head_pic.indexOf("http://") != -1)
        {
            Myappalication.getGlideManager().inputImage(item.head_pic, holder.mImg);
        }else {
            Myappalication.getGlideManager().inputImage(Api.PIC_URL + item.head_pic, holder.mImg);
        }
        holder.mName.setText(item.shop_name);
        holder.mCreatTime.setText(item.ctime);
        holder.mNum.setText("￥" + item.total_price );

        return view;
    }

    class ViewHolder{
        /**
         * 商家图片
         */
        ImageView mImg;

        /**
         * 商家名称
         */
        TextView mName;

        /**
         * 订单创建时间
         */
        TextView mCreatTime;

        /**
         * 订单交易金额
         */
        TextView mNum;



    }


    //刷新数据
    public void refreshData(List<OrderData.OrderList> list, boolean refreshOrLoad) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

}
